package com.example.administrator.androidapp;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class Ch6Activity2 extends AppCompatActivity {
    private ArrayList arrayList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch6_2);


        Spinner spinner=(Spinner)findViewById(R.id.ch6_2_spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Resources resources=getResources();
                String[] arr=resources.getStringArray(R.array.strArr1);
                String selected=arr[position];
                Toast.makeText(Ch6Activity2.this,selected,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //初始数据
        arrayList=new ArrayList();
        arrayList.add("item1");
        arrayList.add("item2");
        arrayList.add("item3");

        Spinner spinner2=(Spinner)findViewById(R.id.ch6_2_spinner2);
        //数据适配器，包含了数据和显示数据的界面
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
        spinner2.setAdapter(arrayAdapter);


        ListView listView=(ListView) findViewById(R.id.ch6_2_lv);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String selected=(String) arrayList.get(position);
                Toast.makeText(Ch6Activity2.this,selected,Toast.LENGTH_LONG).show();
            }
        });
    }
}
